﻿using System.Collections.Generic;
using System.Data.Entity;
using TestData;
using System.Data.Entity.Infrastructure;
using SocialFront.Core.Repository.EF;
using SocialStoreFront.Domain.Concrete;

namespace SocialFront.Tests
{
    public class TestDataInitializer : DropCreateDatabaseAlways<SocialFrontDataContext>
    {
        protected override void Seed(SocialFrontDataContext context)
        {
            var uow = new EFUnitOfWork();
            uow.DataContext.Configuration.ProxyCreationEnabled = true;
            var oCtx = ((IObjectContextAdapter)uow.DataContext).ObjectContext;
            oCtx.ContextOptions.ProxyCreationEnabled = true;

            RandomData.Init();

            List<Customer> customers = new List<Customer>();
            List<Product> products = new List<Product>();
            List<Order> orders = new List<Order>();
            for (int i = 0; i < 1000; i++)
            {
                
                int num = RND.GetRandomInt(1, 3);
                string product = RandomData.GetGrocery();
                for (int j = 0; j <  num; j ++)
                {
                    var adjective = RandomData.GetAdjective();
                    product = string.Format("{0} {1}", adjective, product);
                }


                products.Add(
                    new Product {Name = product, Price = (decimal) RND.GetRandomDouble(5, 500)}
                    );
            }
            for (int i = 0; i < 1000; i++)
            {
                var person = RandomData.GetRandomPerson();
                customers.Add(new Customer { 
                    FirstName = person.FirstName,
                    LastName  = person.LastName ,
                    Address1  = person.Address.Address1 ,
                    City      = person.Address.City ,
                    State     = person.Address.State,
                    Zip       = person.Address.ZipCode                     
                });

            }
            int cnt = 0;
            int idx = 0;
            foreach (var customer in customers)
            {
                if (cnt > 500)
                    idx = cnt - 100;
                if (cnt < 500)
                    idx = cnt + 100;

                for (int i = 0; i < 5; i++)
                {
                    var order = new Order();
                    order.Customer = customer;
                    order.Products = new List<Product>();
                    for (int j = 0; j < 5; j++)
                    { 
                    var product = products[idx];
                    order.Products.Add(product);
                    idx += 1;
                    }
                    
                    orders.Add(order);
                }

                cnt += 1;
            }

            var customersDb = oCtx.CreateObjectSet<Customer>();
            var productsDb = oCtx.CreateObjectSet<Product>();
            var orderDb = oCtx.CreateObjectSet<Order>();

            products.ForEach(c => productsDb.AddObject(c));
            customers.ForEach(c => customersDb.AddObject(c));
            orders.ForEach(c => orderDb.AddObject(c));
            oCtx.SaveChanges();
            //customers.ForEach(m => context.Customers.Add(m));
            //products.ForEach(m => context.Products.Add(m));
            //context.SaveChanges();
        }
    }
}
